//
//  LightsTableVC.m
//  SuperLight
//
//  Created by Charlie Liao on 14-4-24.
//  Copyright (c) 2014年 Charlie Liao. All rights reserved.
//

#import "LightsTableVC.h"

@interface LightsTableVC ()
{
    LightsHeadView *headView;
}

@end

@implementation LightsTableVC

- (void)viewDidLoad
{
    common = [Common share];
    
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self setupNavigation];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [LightController shareInstance].delegate = self;
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [LightController shareInstance].delegate = nil;
}

#pragma mark  设置 View
/**
 * 设置导航栏
 */
- (void)setupNavigation
{
    self.navigationController.navigationBar.translucent = YES;
    self.title = @"List";
    
    UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onClickSearchBtn)];
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onClickAddBtn)];
    
    self.navigationItem.leftBarButtonItems = @[searchBtn, addBtn];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onClickDoneBtn)];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return common.lightsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LightsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LightsCell"];
    
    if (cell == nil) {
        NSArray *nibArr = [[NSBundle mainBundle] loadNibNamed:@"LightsCell" owner:self options:nil];
        cell = (LightsCell*)nibArr[0];
        [cell.selectSwitch addTarget:self action:@selector(cellSwitchChanged:) forControlEvents:UIControlEventValueChanged];
    }
    
    // Configure the cell...
    LightEntity *entity = (LightEntity*)common.lightsArr[indexPath.row];
    
    cell.addrLabel.text = entity.addr;
    cell.nameLabel.text = entity.name;
    cell.selectSwitch.on = entity.isSelect;
    cell.selectSwitch.tag = indexPath.row;
    return cell;
}

#pragma mark  Table view data Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (!headView) {
        NSArray *nibArr = [[NSBundle mainBundle] loadNibNamed:@"LightsCell" owner:self options:nil];
        headView = nibArr[1];
        [headView.selectSwitch addTarget:self action:@selector(checkAllSwitchChanged:) forControlEvents:UIControlEventValueChanged];
    }
    
    return headView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Update", nil];
    sheet.tag = indexPath.row;
    [sheet showInView:self.view];
    
}

#pragma mark  UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0: {
            LightEntity *entity = (LightEntity*)common.lightsArr[actionSheet.tag];
            [common.lightsArr removeObject:entity];
            [self.tableView reloadData];
        }
            break;
            
        case 1: {
            LightsInfoVC *vc = [[LightsInfoVC alloc] initWithNibName:@"LightsInfoVC" bundle:nil];
            vc.type = modificationInfoType;
            LightEntity *entity = (LightEntity*)common.lightsArr[actionSheet.tag];
            vc.index = actionSheet.tag;
            vc.name = entity.name;
            vc.addr = entity.addr;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark Light Msg Delegate

- (void)findTheLight:(msg_pkt)msg
{
    int addr;
    memcpy(&addr, msg.addr, 4);
    NSString *addrStr = [NSString stringWithFormat:@"%X", addr];
    LightEntity *entity = [[LightEntity alloc] initWithName:addrStr AndAddr:addrStr];
    [[Common share].lightsArr addObject:entity];
    [self.tableView reloadData];
}

#pragma mark  Action

- (void)onClickDoneBtn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onClickAddBtn
{
    LightsInfoVC *vc = [[LightsInfoVC alloc] initWithNibName:@"LightsInfoVC" bundle:nil];
    vc.type = addInfoType;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onClickSearchBtn
{
    [[LightController shareInstance] searchLights];
}

- (void)checkAllSwitchChanged:(UISwitch*)sender
{
    for (LightEntity *entity in common.lightsArr) {
        entity.isSelect = sender.on;
    }
    [self.tableView reloadData];
}

- (void)cellSwitchChanged:(UISwitch*)sender
{
    LightEntity *entity = common.lightsArr[sender.tag];
    entity.isSelect = sender.on;
}

@end


#pragma mark - LightsCell

@implementation LightsCell



@end


#pragma mark - LightsHeadView

@implementation LightsHeadView



@end